﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Lion.Data;
using Forest.Lion.Domain.Contracts;
using Forest.Lion.Domain.Models;
using Forest.Lion.Model;
using Forest.Lion.Infrastructure;
using Forest.Lion.Domain.Shared;

namespace Forest.Lion.Domain.Handlers
{
    public class JobPosiotionHandler
    {
        private readonly IJobPositionRepository _jobPositionRepository;
        private readonly IDepartmentRepository _departmentRepository;

        public JobPosiotionHandler(IJobPositionRepository jobPositionRepository, IDepartmentRepository departmentRepository)
        {
            _jobPositionRepository = jobPositionRepository;
            _departmentRepository = departmentRepository;
        }

        #region Create
        public void Create(IJobPositionFormModel jobPositionForm, Guid companyId)
        {
            if (jobPositionForm == null)
                throw new ArgumentNullException("jobPositionForm");

            try
            {
                var entity = jobPositionForm.ConvertToEntity();
                entity.Id = Guid.NewGuid();
                entity.CompanyId = companyId;
                entity.DateCreated = DateTime.Now;
                entity.StartDate = DateTime.Now;
                entity.LastUpdated = DateTime.Now;
                entity.EndDate = DateTime.Now.AddDays(jobPositionForm.DateRage);
                entity.CreatedBy = companyId;
                entity.IsExchange = false;
                entity.Source = 0;
                _jobPositionRepository.Create(entity);
            }
            catch (InvalidOperationException ex)
            {
                throw new BusinessServicesException("UnableToCreateVehicleExceptionMessage", ex);
            }
        }
        #endregion

        #region DeleteById
        public virtual void DeleteById(Guid jobPositionId)
        {
            var JobPositionToDelete = _jobPositionRepository.GetJobPosition(jobPositionId);
            if (JobPositionToDelete != null)
            {
                _jobPositionRepository.Delete(jobPositionId);
            }
        }
        #endregion

        #region GetById
        public JobPosition GetById(Guid jobPositionId)
        {
            return _jobPositionRepository.GetJobPosition(jobPositionId);
        }
        #endregion

        #region GetListByCompanyId
        public IPagedList<JobPosition> SearchList(Guid companyId,
            String keyword,
            int pageIndex,
            int pageSize)
        {
            return _jobPositionRepository.SearchList(companyId, keyword, pageIndex, pageSize);
        }
        #endregion

        #region Update
        public void Update(IJobPositionFormModel jobPositionForm)
        {
            var entity = _jobPositionRepository.GetJobPosition(jobPositionForm.Id);
            jobPositionForm.ConvertToEntity(entity);
            if (entity.Status != (int)Status.Active) 
            {
                entity.StartDate = DateTime.Now;
                entity.EndDate = DateTime.Now.AddDays(jobPositionForm.DateRage);
            }
            entity.LastUpdated = DateTime.Now;
            _jobPositionRepository.Update(entity);
        }
        #endregion
    }
}
